﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class SearchAmount : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }

            else if (!this.IsPostBack)
            {
                this.LoadData();
            }
        }

        protected void LoadData()
        {

            try
            {
                BEGDC.PreContract.BLL.IKeyProjectDefaultAmount amount = (BEGDC.PreContract.BLL.IKeyProjectDefaultAmount)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_KeyProjectDefaultAmount_Service];
                BEGDC.PreContract.DAL.KeyProjectDefaultAmount defaultAmount = amount.GetKeyProjectDefaultAmount(1);
                this.AmountTextBox.Text = defaultAmount.DefaultAmount.ToString();
                if (defaultAmount.UpdateDate != Nullables.NullableDateTime.Default)
                {
                    DateTime date = System.Convert.ToDateTime(defaultAmount.UpdateDate.Value);
                    this.UpdateDateLabel.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                }
  
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void SureButton_Click(object sender, EventArgs e)
        {
            try
            {
               

                BEGDC.PreContract.BLL.IKeyProjectDefaultAmount amount = (BEGDC.PreContract.BLL.IKeyProjectDefaultAmount)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_KeyProjectDefaultAmount_Service];
                BEGDC.PreContract.DAL.KeyProjectDefaultAmount defaultAmount = amount.GetKeyProjectDefaultAmount(1);

                Nullables.NullableDecimal defaultAmountNow = Convert.ToDecimal(this.AmountTextBox.Text);

                if (amount.UpdateKeyProjectDefaultAmount(1, defaultAmountNow, new Nullables.NullableDateTime(System.DateTime.Now)))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "')</script>");
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }

        }
    }
}
